package au.edu.swin.swinvehicle.manager;

/**
 * Manager entity
 * @version 1.0
 * @author Dany Gunawan
 * @lastmodify 2009-05-12
 */
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "MANAGER")
@NamedQueries({
    @NamedQuery(name = "findAllManagers",
    query = "SELECT m FROM ManagerEntity m")
})
public class ManagerEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(length = 60, unique = true, nullable = false)
    private String name;
    @Column(length = 255)
    private String comment;

    // Here, we put it in the vehicleBookings, because every vehicleBooking will always have a manager
    // for the driver and vehicle assignment task
    @OneToMany(mappedBy = "manager", cascade = CascadeType.ALL)
    private List<VehicleBookingEntity> vehicleBookings;

    /**
     * Default constructor
     */
    public ManagerEntity() throws ManagerManagerException {
        try {
            vehicleBookings = new ArrayList<VehicleBookingEntity>();
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot instantiate new ManagerEntity object", e);
        }
    }

    public ManagerEntity(String name, String comment) throws ManagerManagerException {
        this(); // Call default constructor
        try {
            setName(name);
            setComment(comment);
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot instantiate new ManagerEntity object", e);
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public List<VehicleBookingEntity> getVehicleBookings() {
        return vehicleBookings;
    }

    public void setVehicleBookings(List<VehicleBookingEntity> vehicleBookings) {
        this.vehicleBookings = vehicleBookings;
    }

    // Transfer Object Methods
    public ManagerTO getData() throws ManagerManagerException {
        ManagerTO to = null;

        try {
            to = new ManagerTO(id, name);
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot get ManagerTO data", e);
        }

        return to;
    }

    public ManagerDetailedTO getDetailedData() throws ManagerManagerException {
        ManagerDetailedTO to = null;

        try {
            to = new ManagerDetailedTO(id, name, comment);
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot get ManagerDetailedTO data", e);
        }

        return to;
    }

    // Only the ManagerDetailedTO can be used to update the state of the manager entity
    public void setData(ManagerDetailedTO to) throws ManagerManagerException {
        try {
            setId(to.getId());
            setName(to.getName());
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot set ManagerDetailedTO data", e);
        }
    }

    @Override
    public String toString() {
        return "au.edu.swin.swinvehicle.manager.ManagerEntity[id=" + id + "]";
    }
}
